package com.tong.embeddedvr.controller;

import com.tong.embeddedvr.dto.UserInputDTO;
import com.tong.embeddedvr.dto.UserOutputDTO;
import com.tong.embeddedvr.service.impl.UserServiceImpl;
import com.tong.embeddedvr.utils.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;

@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserServiceImpl userServiceImpl;

    @RequestMapping("/sendRegisterMsg")
    public String sendMsg(HttpSession session, UserInputDTO userInputDTO){
        return userServiceImpl.sendRegisterMsg(session,userInputDTO);
    }

    @RequestMapping("/sendLoginMsg")
    public String sendLoginMsg(HttpSession session, UserInputDTO userInputDTO){
        return userServiceImpl.sendLoginMsg(session,userInputDTO);
    }

    @RequestMapping("/register")
    public String register(HttpSession session, UserInputDTO userInputDTO){
        return userServiceImpl.register(userInputDTO, session);
    }

    @RequestMapping("/login")
    public String login(HttpSession session,UserInputDTO userInputDTO){
        return userServiceImpl.login(session,userInputDTO);
    }

    @RequestMapping("/getuser")
    public String getUser(HttpSession session){
        UserOutputDTO user = (UserOutputDTO) session.getAttribute("user");
        return CommonUtil.ajaxReturn(user);
    }

    @RequestMapping("/update")
    public String updateInformation(HttpSession session,UserInputDTO userInputDTO){
        UserOutputDTO user = (UserOutputDTO) session.getAttribute("user");
        userInputDTO.setUserId(user.getUserId());
        return userServiceImpl.updateInformation(userInputDTO);
    }

    @RequestMapping("/delete")
    public String delete(HttpSession session,UserInputDTO userInputDTO){
        UserOutputDTO user = (UserOutputDTO)session.getAttribute("user");
        userInputDTO.setUserIsDelete(1);
        return userServiceImpl.updateInformation(userInputDTO);
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return CommonUtil.ajaxReturn(200,"注销成功");
    }

    @RequestMapping("/test")
    public String test(HttpSession session) throws Exception {
        return CommonUtil.ajaxReturn("test成功");
    }
}
